package com.example.mvp.view.slidebar;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.example.baselibrary.R;
import com.example.inter.ItemClickInterface;
import com.example.mvp.bean.ClassMemberInfoEntity;
import com.example.mvp.view.GlideCircleTransform;
import com.example.mvp.view.LKCircleImageView;
import com.example.mvp.view.slidebar.util.SortModel;

import java.util.ArrayList;
import java.util.List;

import cc.vv.lklibrary.log.LogOperate;

public class RecCarAdapter extends RecyclerView.Adapter<RecCarAdapter.Holder> {

    private List<ClassMemberInfoEntity> mList;

    private Activity mActivity;

    private ItemClickInterface itemClickInterface;
    private RequestOptions mHeadRequestOptions = new RequestOptions();
    public RecCarAdapter(Activity activity) {
        mList = new ArrayList<>();
        mActivity = activity;
    }

    public void addDatas(List<ClassMemberInfoEntity> data) {
        this.mList.clear();
        this.mList.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rec_car, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        ClassMemberInfoEntity car = mList.get(position);
        holder.tv_irc_nick.setText(car.getNickName());
        LogOperate.e("头像=="+car);
        setHead(mActivity, car.getAvatar(),holder.ima_irc_avatar );
        holder.ll_irc_rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClickInterface!=null){
                    itemClickInterface.itemClick(position);
                }

            }
        });

    }

    /**
     * 设置点击监听
     * @param itemClickInterface
     */
  public void setItemClickInterface(ItemClickInterface itemClickInterface){
      this.itemClickInterface=itemClickInterface;
  }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class Holder extends RecyclerView.ViewHolder {
        /**
         * 昵称
         */
        TextView tv_irc_nick;
        /**
         * 头像
         */
        LKCircleImageView ima_irc_avatar;
        /**
         * 根布局
         */
        LinearLayout ll_irc_rootView;


        Holder(View itemView) {
            super(itemView);
            tv_irc_nick= itemView.findViewById(R.id.tv_irc_nick);
            ima_irc_avatar= itemView.findViewById(R.id.ima_irc_avatar);
            ll_irc_rootView=itemView.findViewById(R.id.ll_irc_rootView);
        }

    }

    /**
     * 设置头像和昵称
     */
    private void  setHead(Context context, String headUrl,LKCircleImageView headImage ){
        Object tag = headImage.getTag(R.id.tag_head);
        if ( tag==null||!tag.equals(headUrl)){
            RequestManager mRequestManager = Glide.with(context);
            mHeadRequestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
            mHeadRequestOptions.skipMemoryCache(true);
            mHeadRequestOptions.error(R.mipmap.icon_defout_head);
            mHeadRequestOptions.transforms(new GlideCircleTransform(context));
            mRequestManager.load(headUrl)
                    .thumbnail(0.2f)
                    .apply(mHeadRequestOptions)
                    .into(headImage);
            headImage.setTag(R.id.tag_head,headUrl);

        }

    }
}
